exports.bargain_home_share = function(req,res) {

    //获取restful参数
    var restfulParams = req.routeInfo.restfulParams;

    //最终结果
    var resultData = {};

    //任务流创建
    async.waterfall([

            //获取砍价活动信息
            function(cb){

                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.search_organization_activity_info + "/oa_id/" + restfulParams.oa_id,
                        method: 'GET',
                    },
                    function (err, data) {

                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }
                        if(data.data.length>0){
                            resultData.activityData = data.data[0];
                        }else{
                            resultData.activityData = "delete";
                        }

                        cb();
                    }
                );


            },

            //查询砍价活动的扩展信息
            function(cb){

                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchBargainActivityConfig + "/bc_oaid/" + restfulParams.oa_id,
                        method: 'GET',
                    },
                    function (err, data) {
                        //console.log(data);
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }
                        resultData.activityConfigData = data.data[0];

                        cb();
                    }
                );

            },

            //查看砍价活动内容
            function(cb){

                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchBargainActivityContent + "/bc_oaid/" + restfulParams.oa_id,
                        method: 'GET',
                    },
                    function (err, data) {
                        //console.log(data);
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }
                        resultData.activityContentData = data.data;

                        cb();
                    }
                );

            },

            //获取荣誉榜前10
            function(cb){

                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchBargainJoinUserInfo +  "/bj_oaid/" + restfulParams.oa_id + "/sort_order/bj_get_cut_price-desc/skip/0/limit/10",
                        method: 'GET',
                    },
                    function (err, data) {
                        //console.log(data);
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        resultData.rankingData = data.data;

                        cb();

                    }
                );

            },
            function(cb){

                var tasks = [];

                //循环节点列表
                resultData.rankingData.forEach(function (item, i) {
                    //创建每一个插入任务
                    tasks.push(function (cb) {

                        item.now_price = parseFloat(parseFloat(item.bc_most_price) - parseFloat(item.bj_get_cut_price)).toFixed(2);

                        cb();

                    });

                });

                async.waterfall(tasks, cb);


            },
            //获取参加活动的总人数
            function(cb){

                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchBargainJoin +  "/count/1/bj_oaid/" + restfulParams.oa_id ,
                        method: 'GET',
                    },
                    function (err, data) {
                        //console.log(data);
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        resultData.joinCount = data.count;

                        cb();

                    }
                );

            },

            //获取分享者的砍价信息
            function(cb){



                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchBargainJoinUserInfo + "/bj_uid/" + restfulParams.share_uid + "/bj_oaid/" + restfulParams.oa_id,
                        method: 'GET',
                    },
                    function (err, data) {
                        //console.log(data);
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }
                        resultData.joinData = data.data;
                        cb()
                    }
                );


            },
            //浏览数加一
            function(cb){

                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.browse_count_setInc ,
                        method: 'POST',
                        data: {
                            oid:  restfulParams.oa_id,
                        }
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        resultData.isaddone =true;

                        cb();
                    }
                );



            },
            //判断用户是否收藏了这个活动
            function(cb){

                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.userSearchCollect +  "/is_delete/0/uc_type/1/uc_fid/" + restfulParams.oa_id + "/uc_uid/" + restfulParams.uid,
                        method: 'GET',
                    },
                    function (err, data) {
                        // //console.log(data);
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }
                        if(data.data.length>0){
                            resultData.isCollect = 1;
                        }else{
                            resultData.isCollect = 0;
                        }
                        cb();

                    }
                );

            },

            function(cb){

                ////console.log(CORE_APIConfig.coreAPI.searchBargainJoinUserInfo + "/bj_uid/" + restfulParams.u_id + "/bj_oaid/" + restfulParams.oa_id)

                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchBargainJoinUserInfo + "/bj_is_complete/0/bj_uid/" + restfulParams.uid + "/bj_oaid/" + restfulParams.oa_id,
                        method: 'GET',
                    },
                    function (err, data) {
                        //console.log(123);
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }
                        //判断该用户是否参与砍价活动了
                        if(data.data.length <= 0){
                            resultData.isParticipate = false;
                            // return RES.response(res,true,resultData);
                            cb();
                        }else{
                            resultData.isParticipate = true;
                            cb();
                        }

                    }
                );


            },
            //查询参加活动人的信息
            function(cb){

                ////console.log(CORE_APIConfig.coreAPI.searchBargainJoinUserInfo + "/bj_uid/" + restfulParams.u_id + "/bj_oaid/" + restfulParams.oa_id)
                if(restfulParams.bj_oid == 0){
                    R.SEND_HTTP(
                        req,
                        {
                            url: CORE_APIConfig.coreAPI.searchBargainJoinUserInfo + "/bj_is_complete/0/bj_uid/" + restfulParams.share_uid + "/bj_oaid/" + restfulParams.oa_id,
                            method: 'GET',
                        },
                        function (err, data) {
                            //console.log(123);
                            //判断是否有错误信息
                            if (err) {
                                return cb(err, data);
                            }
                            if(data.data.length > 0){
                                resultData.joinData = data.data;
                                cb();
                            }else{
                                cb();
                            }

                        }
                    );
                }else{
                    R.SEND_HTTP(
                        req,
                        {
                            url: CORE_APIConfig.coreAPI.searchBargainJoinUserInfo + "/bj_oid/" + restfulParams.bj_oid,
                            method: 'GET',
                        },
                        function (err, data) {
                            //console.log(123);
                            //判断是否有错误信息
                            if (err) {
                                return cb(err, data);
                            }
                            if(data.data.length > 0){
                                resultData.joinData = data.data;
                                cb();
                            }else{
                                cb();
                            }

                        }
                    );
                }

            },
            //判断该砍价活动用户有没有有未支付的订单（如有，那么返回订单id）
            function(cb){
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.search_order + "/count/1/o_pay_status/0/o_type/0/o_activity_type/2/is_hide/0/o_aoc_id/" + restfulParams.oa_id+"/o_uid/" +restfulParams.uid,
                        method: 'POST',
                        data: {
                            fields: ["o_id"].join(",")
                        }
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }
                        if(data.data.length>0){
                            resultData.is_not_pay_order = data.data[0].o_id;
                        }else{
                            resultData.is_not_pay_order = 0;
                        }



                        cb();
                    }
                );
            },




            //相关数据计算
            function(cb){

                //帮砍价的好友人数
                if(resultData.joinData.length>0){
                    resultData.helpCount = resultData.joinData[0].bj_get_cut_count;
                    //是否已经下单
                    resultData.is_order = resultData.joinData[0].bj_is_order;
                    resultData.cutTotalMoney = resultData.joinData[0].bj_get_cut_price;
                    //当前的价格
                    resultData.nowMoney =  parseFloat(parseFloat(resultData.activityConfigData.bc_most_price) - parseFloat(resultData.cutTotalMoney)).toFixed(2);

                }else{
                    cb();
                }

                //已经砍掉的钱数

                //目标价
                resultData.targetMoney = resultData.activityConfigData.bc_least_price;

                //原价
                resultData.oldMoney = resultData.activityConfigData.bc_most_price;


                cb();


            }


        ],function(err, result){

            //返回最终拼接结果
            RES.response(res,true,resultData);

        }
    );

};